package com.sun.showcase.controller.mall;

import com.sun.showcase.biz.mall.service.ShopService;
import com.sun.showcase.client.domain.mall.Shop;
import com.sun.showcase.client.query.mall.ShopQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/mall/shop")
public class ShopController{
	
	@Autowired
	private ShopService shopService;
	
	private Result result = new Result();
	/**
	 * 跳转到Shop管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goShop")
	public String goShop() {
		return "mall/shop";
	}
	/**
	 * 跳转到Shop新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleShop")
	public String handleShop(@RequestParam(value="id",required=false) Long id , ModelMap map) {
		return "mall/shop_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public Shop showDesc(ShopQuery shopQuery) {
		return shopService.get(shopQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(ShopQuery shopQuery) {
		return shopService.datagrid(shopQuery);
	}
	

	/**
	 * 添加一个Shop
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(ShopQuery shopQuery) {
		ShopQuery q = new ShopQuery();
	    q.setUserId(shopQuery.getUserId());
	    List<ShopQuery> list = shopService.listAll(q);
	    if(list.size()>0) {
	    	result.setSuccess(false);
			result.setMsg("此用户已拥有店铺");
			return result;
	    }
		shopService.add(shopQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑Shop
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(ShopQuery shopQuery) {
		ShopQuery q = new ShopQuery();
	    q.setUserId(shopQuery.getUserId());
	    List<ShopQuery> list = shopService.listAll(q);
	    if((list.size() == 1 && !list.get(0).getId().equals(shopQuery.getId()))||list.size()>1) {
	    	result.setSuccess(false);
			result.setMsg("此用户已拥有店铺");
			return result;
	    }
		shopService.update(shopQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}

	/**
	 * 物理删除Shop
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(ShopQuery shopQuery) {
		shopService.delete(shopQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	
	/**
	 * 逻辑删除Shop
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Result deletePt(ShopQuery shopQuery) {
		shopService.deletePt(shopQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	/**
	 * 全部数据Shop
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(ShopQuery shopQuery) {
		List<ShopQuery> list = shopService.listAll(shopQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
}
